import QtQuick 1.0

Rectangle {
    property bool moving      : false
    property int direction    : 1
    property int speed        : 3

    property int countTimeOut : 0
    property int timeOut      : 30

    property string level: ""

    property int startX : 0
    property int startY : 0

    property bool paused : true

    width: 60
    height: 80

    z: 3

    color: "red"

    id: playerObj

    Component {
        id: ouroborosAnimation

        Ouroboros { }
    }

    onPausedChanged: {
        //console.debug("mudou paused do player")
    }

    Component.onCompleted: {
        x = startX;
        y = startY;
    }

    function collectItem() {
        playerContext.collectedItems++;
    }


    function checkCollisionWithChest(obj) {
        if (obj.parent.hasItem) {
          //obj.parent.visible = false;
            obj.parent.destroy();
          collectItem();
        }
    }

    function checkCollisionWithEnemy(obj) {
        if(!obj.parent.dead) {
            if (playerContext.atacking) {
                    obj.parent.health = obj.parent.health -
                        (playerContext.strength > obj.parent.defense ? (playerContext.strength - obj.parent.defense) : 0);
            }
            if(obj.parent.attacking){
                playerContext.health = playerContext.health -
                        (obj.parent.strength > playerContext.defense ? (obj.parent.strength - playerContext.defense) : 0);
            }

            if(obj.parent.health <= 0) {
                obj.parent.dead = true;
            }
        }
    }

    function returnToCastleLevel() {
        world.level = "castleLevel"
        castleLevel.resetPositions();
    }

    function returnToCityLevel() {
        world.level = "cityLevel"
        cityLevel.resetPositions();
    }

    function playerMovement() {
        if (player.visible && !playerContext.dead) {
            var obj = world.checkObjectCollision(player);
            if (obj.length > 0) {
                for (var i = 0; i < obj.length; i++) {
                    if (obj[i].parent.visible) {
                        if (obj[i].objectName == "portal") {
                            switch (level) {
                            case "cityLevel":
                                returnToCastleLevel();
                                break;
                            case "castleLevel":
                                returnToCityLevel();
                                break;
                            }
                        }
                        else if (obj[i].objectName == "boss-portal") {
                            if (level == "castleLevel") {
                                if (playerContext.totalHealth == 18) {
                                    world.level = "dungeonLevel";
                                }
                                else {
                                    //console.debug("trying to enter man!");
                                    updatePositionX(-1);
                                }
                            }
                        }
                    }
                }
            }
        }
    }

    function updatePositionX(amount) {
        var scroll = ((direction == 1 && player.x + map.x > world.width * 0.6 && -map.x < map.width - world.width) ||
                      (direction == -1 && player.x + map.x < world.width * 0.4 && map.x < 0));

        if (scroll) {

            switch (level) {
            case "cityLevel":
                cityLevel.scroll(amount);
                break;
            case "castleLevel":
                castleLevel.scroll(amount);
                break;
            case "dungeonLevel":
                dungeonLevel.scroll(amount);
                break;
            }
        }
         if( player.x + amount <= 0) {
             player.x = 0;
         }  else if (player.x + player.width + amount > map.width) {
             player.x = map.width - player.width ;
         }else {
            player.x += amount;
         }
    }

    Item {
        //focus: true
        focus: (world.level == level && !playerObj.paused)

        Keys.onPressed: {
            if (event.isAutoRepeat)
                return;

            if (event.key == Qt.Key_Right) {
                if (!player.moving) {
                    player.direction = 1;
                    player.moving = true;
                    movementTimer.start();
                }
            }

            if (event.key == Qt.Key_Left) {
                if (!player.moving) {
                    player.direction = -1;
                    player.moving = true;
                    movementTimer.start();
                }
            }

            if (event.key == Qt.Key_Space) {
                playerContext.atacking = true;

            }

            if (event.key == Qt.Key_Up) {
                var obj = world.checkObjectCollision(player);
                if (obj.length > 0) {
                    for (var i = 0; i < obj.length; i++) {
                        if (obj[i].objectName == "chest") {
                            if (obj[i].parent.visible) {
                                checkCollisionWithChest(obj[i]);
                            }
                        }
                    }
                }

           }

            if (event.key == Qt.Key_Control) {
                //drink elixir!
                if (playerContext.collectedItems >= 3) {
                    ouroborosAnimation.createObject(game)

                    playerContext.strength += 1.5;
                    playerContext.defense  += 1.2;
                    playerContext.totalHealth += 3;
                    playerContext.health = playerContext.totalHealth;

                    playerContext.collectedItems = 0;
                    game.resortItems();
                }
            }
        }

        Keys.onReleased: {
            if (event.isAutoRepeat)
                return;

            if (event.key == Qt.Key_Right || event.key == Qt.Key_Left) {
                player.moving = false;
                movementTimer.stop();
            }

            if (event.key == Qt.Key_Space) {
                playerContext.atacking = false;
                attackTimer.start();
            }

            if (event.key == Qt.Key_Up) {
                playerContext.openingChest = false;
            }
        }
    }

    /* Movement timer */
    Timer {
        id: movementTimer
        interval: 25
        repeat: true
        running: false
        onTriggered: {
            if (player.moving) {
                playerMovement();
                updatePositionX(player.direction * player.speed)
            }
        }
    }

    Timer {
        id: attackTimer
        interval: 750
        repeat: true
        running: !playerObj.paused
        onTriggered: {

            var obj = world.checkObjectCollision(player);
            if (obj.length > 0) {
                for (var i = 0; i < obj.length; i++) {
                    if (obj[i].objectName == "enemy" || obj[i].objectName == "boss") {
                        checkCollisionWithEnemy(obj[i]);
                    }
                }
            }

            if (playerContext.attacking) {
                attacking = false;

            }
        }
    }

    Timer {
        id: looseLife
        interval: 15000
        repeat: true
        running: !playerObj.paused
        onTriggered: {
            playerContext.health = playerContext.health - 1;
            if(playerContext.health <= 0) {
                playerContext.dead = true;
            }

        }
    }
}
